{ "cells": [ { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "application/javascript": [ "\n", " var component = document.getElementById(\"sketch_7\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"state_7\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"controls_div_7\");\n", " if (component != undefined)\n", " component.remove();\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " // FIXME: Stop all previously running versions (?)\n", " var processingInstance = Processing.getInstanceById(\"canvas_7\");\n", " if (processingInstance != undefined && processingInstance.isRunning())\n", " processingInstance.noLoop();\n", " });\n", "\n", "\n", " var output_area = this;\n", " // find my cell element\n", " var cell_element = output_area.element.parents('.cell');\n", " // which cell is it?\n", " var cell_idx = Jupyter.notebook.get_cell_elements().index(cell_element);\n", " // get the cell object\n", " var cell = Jupyter.notebook.get_cell(cell_idx);\n", "\n", " function jyp_print(cell, newline) {\n", " return function(message) {\n", " cell.get_callbacks().iopub.output({header: {\"msg_type\": \"stream\"},\n", " content: {text: message + newline,\n", " name: \"stdout\"}});\n", " }\n", " }\n", " window.jyp_println = jyp_print(cell, \"\\n\");\n", " window.jyp_print = jyp_print(cell, \"\");\n", "\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " Processing.logger.println = jyp_print(cell, \"\\n\");\n", " Processing.logger.print = jyp_print(cell, \"\");\n", " });\n", "\n", "\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "
\n", " Sketch #7:
\n", "
\n", "
\n", "
\n", " \n", " \n", " \n", " \n", "
\n", "Sketch #7 state: Loading...
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "int guessCount;\n", "String stage;\n", "int guessMin;\n", "int guessMax;\n", "\n", "void setup() {\n", " size(600, 100);\n", " guessCount = 0;\n", " textFont(createFont(\"Arial\", 20));\n", " stage = \"start\";\n", " guessMin = 0;\n", " guessMax = 1000000;\n", "}\n", "\n", "void draw() {\n", " background(0);\n", " if (stage.equals(\"start\")) {\n", " // display an intro message\n", " text(\"Welcome to AI! Think of a number (\" + guessMax + \") then press SPACE\", 10, 50);\n", " } else if (stage.equals(\"play\")) {\n", " // ask user for for feed back\n", " text(\"Here is my guess: \" + currentGuess() + \".... press L or H or C\", 10, 50);\n", " } else {\n", " // computer won or lost\n", " text(\"GAME OVER! It took me \" + guessCount + \" tries!\", 10, 50);\n", " }\n", "}\n", "\n", "int currentGuess() {\n", " return int((guessMin + guessMax) / 2.0);\n", "}\n", "\n", "void keyPressed() {\n", " if (key == 'h' || key == 'H') {\n", " // guess higher!\n", " guessMin = currentGuess();\n", " guessCount++;\n", " } else if (key == 'l' || key == 'L') {\n", " // guess lower!\n", " guessMax = currentGuess();\n", " guessCount++;\n", " } else if (key == 'c' || key == 'C') {\n", " stage = \"game over\";\n", " } else if (key == ' ') {\n", " // start playing!\n", " stage = \"play\";\n", " } \n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Calysto Processing", "language": "java", "name": "calysto_processing" }, "language_info": { "codemirror_mode": { "name": "text/x-java", "version": 2 }, "file_extension": ".java", "mimetype": "text/x-java", "name": "java" } }, "nbformat": 4, "nbformat_minor": 2 }